package com.aier.service.inventory;


import com.aier.bean.inventory.ProductEntryBean;
import com.aier.bean.common.CommonResult;
import com.aier.bean.common.GlobalCode;
import com.aier.bean.common.OperateType;
import com.aier.exception.CastException;
import com.aier.mapper.inventory.ProductEntryMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.List;

/**
 * 商品录入信息 service
 */
@Service
public class ProductEntryService {

    @Resource
    private ProductEntryMapper productEntryMapper;

    /**
     * 获取所有时间的出入库数据,并按时间排序
     *
     * @return
     */
    public List<ProductEntryBean> getAllData() {
        return productEntryMapper.getAllData();
    }

    /**
     * 根据型号，日期获取商品录入信息
     *
     * @return
     */
    public List<ProductEntryBean> getProductEntryByModelNameAndDate(String modelName, LocalDate startDate, LocalDate endDate) {
        return productEntryMapper.getProductEntryByModelNameAndDate(modelName, startDate, endDate);
    }

    /**
     * 修改业务员姓名
     */
    public CommonResult updateSalespersonByModelNameAndOldSalesperson(String modelName, String oldSalesperson, String newSalesperson) {
        try {
            int updateRows = productEntryMapper.updateSalespersonByModelNameAndOldSalesperson(modelName, oldSalesperson, newSalesperson);
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.UPDATE_SALESPERSON_FAIL);
        }
        return CommonResult.success();
    }

    /**
     * 插入商品录入信息
     */
    public CommonResult saveProductEntry(ProductEntryBean productEntryBean){
        if (!productEntryBean.getOperateType().equals(OperateType.inInventory) && !productEntryBean.getOperateType().equals(OperateType.outInventory)){
            CastException.cast(GlobalCode.OPERATE_TYPE_FAIL);
        }
        try {
            productEntryMapper.insert(productEntryBean);
        }catch (Exception e){
            e.printStackTrace();
            CastException.cast(GlobalCode.SAVE_PRODUCT_FAIL);
        }
        return CommonResult.success();
    }
}
